package com.guofen.service.sjq.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guofen.dao.SjqWeightProductionFaecesInfoMapper;
import com.guofen.dao.SjqWeightProductionFaecesMapper;
import com.guofen.pojo.SjqFlocculationProduceCheck;
import com.guofen.pojo.SjqWeightProductionFaeces;
import com.guofen.pojo.SjqWeightProductionFaecesInfo;
import com.guofen.service.sjq.SjqWeightProductionFaecesInfoService;
import com.guofen.util.DateAndTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class SjqWeightProductionFaecesInfoServiceImpl implements SjqWeightProductionFaecesInfoService {

    @Autowired
    SjqWeightProductionFaecesInfoMapper sjqWeightProductionFaecesInfoMapper;

    @Autowired
    SjqWeightProductionFaecesMapper sjqWeightProductionFaecesMapper;

    @Override
    public List<Boolean> addFromFaecesExcel(List<SjqWeightProductionFaecesInfo> sjqWeightProductionFaecesInfoList) {
        List<Boolean> registerResult = new ArrayList<>();
        SjqWeightProductionFaeces sjqWeightProductionFaeces = new SjqWeightProductionFaeces();
        int list1[] = {0,0,0,0};
        double list2[] = {0.0,0.0,0.0,0.0};
        for (SjqWeightProductionFaecesInfo s : sjqWeightProductionFaecesInfoList){
            registerResult.add(sjqWeightProductionFaecesInfoMapper.insert(s) == 1);
            if (s.getCarryCompany().equals("海淀环卫五队")) {list1[0]++; list2[0]+=s.getNetWeight();}
            if (s.getCarryCompany().equals("四清")) {list1[1]++; list2[1]+=s.getNetWeight();}
            if (s.getCarryCompany().equals("宝山")) {list1[2]++; list2[2]+=s.getNetWeight();}
            if (s.getCarryCompany().equals("田丰")) {list1[3]++; list2[3]+=s.getNetWeight();}
        };
        sjqWeightProductionFaeces.setTrain1(list1[0]);
        sjqWeightProductionFaeces.setTrain2(list1[1]);
        sjqWeightProductionFaeces.setTrain3(list1[2]);
        sjqWeightProductionFaeces.setTrain4(list1[3]);
        sjqWeightProductionFaeces.setWeight1(list2[0]);
        sjqWeightProductionFaeces.setWeight2(list2[1]);
        sjqWeightProductionFaeces.setWeight3(list2[2]);
        sjqWeightProductionFaeces.setWeight4(list2[3]);
        String date = DateAndTimeUtil.getDate();
        sjqWeightProductionFaeces.setDate(date);
        sjqWeightProductionFaeces.setTrainTotal(list1[0]+list1[1]+list1[2]+list1[3]);
        sjqWeightProductionFaeces.setWeightTotal(list2[0]+list2[1]+list2[2]+list2[3]);
        registerResult.add(sjqWeightProductionFaecesMapper.insert(sjqWeightProductionFaeces) == 1);
        return registerResult;
    }

    @Override
    public Map<String, Object> get(int pageSize, int currentPage) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        List<SjqWeightProductionFaecesInfo> list = sjqWeightProductionFaecesInfoMapper.selectAll();
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public Map<String, Object> getByTime(int pageSize, int currentPage, long startTime, long endTime) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(currentPage, pageSize);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = dateFormat.format(new Date(startTime));
        String endDate = dateFormat.format(new Date(endTime));
        List<SjqWeightProductionFaecesInfo> list = sjqWeightProductionFaecesInfoMapper.selectByDate(startDate, endDate);
        PageInfo pageRecord = new PageInfo(list);
        long sum = pageRecord.getTotal();
        list = pageRecord.getList();
        map.put("records",list);
        map.put("sum",sum);
        return map;
    }

    @Override
    public int delete(int id) {
        return sjqWeightProductionFaecesInfoMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(SjqWeightProductionFaecesInfo record) {
        return sjqWeightProductionFaecesInfoMapper.updateByPrimaryKeySelective(record);
    }
}
